package com.sgb.kjwl.view.widget;

import android.content.Context;
import android.graphics.Paint;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.NewGoodsDetailEntity;
import com.sgb.kjwl.model.entity.SkuCombinationEntity;
import com.sgb.kjwl.model.entity.user.SkuItemAttrEntity;
import com.sgb.kjwl.utils.PointUtils;
import com.swgk.core.util.UiUtil;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.ui.adapter.shopping.SelfShopAttrAdapter;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.util.ScreenUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

public class SelfGoodAttrWindow extends BasePopWindow {
    private RecyclerView paramsList;
    private TextView tv_close;
    private TextView tv_price;
    private TextView name;
    private SelfShopAttrAdapter goodsParamsAdapter;
    private LinearLayoutManager linearLayoutManager;
    private NewGoodsDetailEntity data;
    private ImageView mIvImg;
    private TextView mTvSelect;
    private TextView tvOldprice;
    private RelativeLayout addDeleteLayout;
    private View line;
    private LinkedHashMap<Integer, String> map = new LinkedHashMap<>();
    private HashMap<String, String> strMap = new HashMap<>();
    private HashMap<HashMap<String, String>, String> price = new HashMap<>();
    private HashMap<HashMap<String, String>, String> old_price = new HashMap<>();
    String totalPrice = "";
    String oldprice = "";
    String attr;
    private String price_money = "";
    private String sku_string = "";
    private List<SkuItemAttrEntity> itemAttrEntityList = new ArrayList<>();

    List<SkuCombinationEntity> list = new ArrayList<>();

    public SelfGoodAttrWindow(Context context) {
        super(context, DIR_DOWN_UP);
    }

    @Override
    protected int popLayout() {
        return R.layout.pop_goods_sku;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        paramsList = holder.getPop().findViewById(R.id.params_list);
        tv_close = holder.getPop().findViewById(R.id.tv_close);
        tv_price = holder.getPop().findViewById(R.id.tv_price);
        tvOldprice = holder.getPop().findViewById(R.id.tv_oldprice);
        addDeleteLayout = holder.getPop().findViewById(R.id.add_delete_layout);
        line = holder.getPop().findViewById(R.id.view_goods_sku_line);
        name = holder.getPop().findViewById(R.id.name);
        mIvImg = holder.getPop().findViewById(R.id.iv_img);
        mTvSelect = holder.getPop().findViewById(R.id.tv_select);
        tv_close.setText("确认");
        if (goodsParamsAdapter == null) {
            goodsParamsAdapter = new SelfShopAttrAdapter(mContext, null);
        }
        goodsParamsAdapter.OnPrice(new SelfShopAttrAdapter.IPrice() {
            @Override
            public void getMoney(String money) {

            }

            @Override
            public void getPosition(int x, int y) {
                String sb = "";
                attr = "";
                map.put(x, itemAttrEntityList.get(x).getList().get(y).getValData());
                for (Integer key : map.keySet()) {
                    sb += map.get(key)+" ";
                    attr += map.get(key) + "、";
                }
                totalPrice = "0.00";
                mTvSelect.setText("已选择:\t" + sb.toString());
                strMap.put(itemAttrEntityList.get(x).getParamData(), itemAttrEntityList.get(x).getList().get(y).getValData());
                if (strMap.size() == data.getCheckedItemAttr().getList().get(0).getList().size()) {
                    for (int i = 0; i < data.getCheckedItemAttr().getList().size(); i++) {
                        HashMap<String, String> childMap = new HashMap<>();
                        for (int j = 0; j < data.getCheckedItemAttr().getList().get(i).getList().size(); j++) {
                            String key = data.getCheckedItemAttr().getList().get(i).getList().get(j).getName();
                            String val = data.getCheckedItemAttr().getList().get(i).getList().get(j).getVal();
                            childMap.put(key, val);
                        }

                        if (data.getCheckedItemAttr().getList().get(i).getPromotePrice() != 0 && data.getCheckedItemAttr().getList().get(i).getPromotePrice() < Double.parseDouble(data.getCheckedItemAttr().getList().get(i).getMoney()) && data.getCheckedItemAttr().getList().get(i).getPromoteStatus() == 2) {
                            price.put(childMap, data.getCheckedItemAttr().getList().get(i).getPromotePrice() + "");
                            old_price.put(childMap, data.getCheckedItemAttr().getList().get(i).getMoney() + "");
                        } else {
                            price.put(childMap, data.getCheckedItemAttr().getList().get(i).getMoney());
                        }
                    }

                    totalPrice = price.get(strMap);
                    if (old_price.get(strMap) != null && !TextUtils.isEmpty(old_price.get(strMap))) {
                        oldprice = old_price.get(strMap);
                    } else {
                        oldprice = "";
                    }
                }
                if (totalPrice != null && !TextUtils.isEmpty(totalPrice)) {
                    tv_price.setText("¥" + PointUtils.price(Double.parseDouble(totalPrice)) + "/" + data.getUnit());
                } else {
                    tv_price.setText("无货");
                }
                if (!TextUtils.isEmpty(oldprice)) {
                    tvOldprice.setVisibility(View.GONE);
                    tvOldprice.setText("¥" + PointUtils.price(Double.parseDouble(oldprice)) + "/" + data.getUnit());
                    tvOldprice.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
                } else {
                    tvOldprice.setVisibility(View.GONE);
                }
                UiUtil.sendMsg(EventConstant.EVENT_SELECT, sb.toString());
            }


        });
        linearLayoutManager = new LinearLayoutManager(mContext);
        paramsList.setLayoutManager(linearLayoutManager);
        paramsList.setAdapter(goodsParamsAdapter);
        // TODO: 2020/12/11 0011
        tv_close.setOnClickListener(v -> {
            dismiss();
            if (onResultClick != null && !TextUtils.isEmpty(totalPrice)) {
                onResultClick.result(totalPrice, attr, oldprice);
            }
        });

        holder.getPop().findViewById(R.id.view_goods_sku_close).setOnClickListener(v -> dismiss());
    }

    public void setData(NewGoodsDetailEntity data, List<SkuItemAttrEntity> itemAttrEntityList, List<SkuCombinationEntity> list) {
        this.data = data;
        addDeleteLayout.setVisibility(View.INVISIBLE);
        line.setVisibility(View.INVISIBLE);
        Glide.with(mContext).load(data.getImages().get(0)).apply(new RequestOptions().centerCrop()).into(mIvImg);
        name.setText(data.getCartTitle());
        this.list = list;
        this.itemAttrEntityList = itemAttrEntityList;
        goodsParamsAdapter.setDatas(itemAttrEntityList);
        goodsParamsAdapter.setCheckData(list);
    }


    public void setData(NewGoodsDetailEntity data, List<SkuItemAttrEntity> itemAttrEntityList, List<SkuCombinationEntity> list, List<NewGoodsDetailEntity.commoditySkuListItem> choicedata, String price, String sku) {
        this.data = data;
        addDeleteLayout.setVisibility(View.GONE);
        Glide.with(mContext).load(data.getImages().get(0)).apply(new RequestOptions().centerCrop()).into(mIvImg);
        name.setText(data.getCartTitle());
        this.list = list;
        this.price_money = price;
        this.sku_string = sku;
        if (mTvSelect != null) {
            mTvSelect.setText("已选择:\t" + sku_string.replace("、", ""));
        }
        if (tv_price != null) {
            tv_price.setText("¥" + price);
        }
        this.itemAttrEntityList = itemAttrEntityList;
        goodsParamsAdapter.setDatas(itemAttrEntityList);
        goodsParamsAdapter.setChoicedata(choicedata);
        goodsParamsAdapter.setCheckData(list);
    }


    @Override
    public void showDownPopwindow(View view, boolean alpha) {
        boolean hasGoodsParamsData = null != goodsParamsAdapter && null != goodsParamsAdapter.getDatas() && goodsParamsAdapter.getDatas().size() > 0;
        int calHeight = hasGoodsParamsData ? (int) (ScreenUtil.getDisplayHeight() * 0.85) : (int) (ScreenUtil.getDisplayHeight() * 0.45);
        setHeight(calHeight);
        super.showDownPopwindow(view, alpha);
    }
}
